//
//  WmcUploadFailureVM.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/2.
//

import Foundation

class WmcUploadFailureVM {
    
    var dataSource: [WmcUploadImageV2Model] = []
    
    typealias WmcUploadTaskCount = (uploadingCount: Int, totalCount: Int)
    
    func query(completion: @escaping (WmcUploadTaskCount) -> Void) {
        WmcUploadQueueV2DBManager.query { list in
            var uploadingCount: Int = 0
            let array: [WmcUploadImageV2Model] = list.map { [weak self] model in
                guard let weakSelf = self else { return model }
                let isUploading = weakSelf.queryIsUploading(model: model)
                model.uploading = isUploading
                if isUploading {
                    uploadingCount += 1
                }
                return model
            }
            self.dataSource = array
            let count: WmcUploadTaskCount = (uploadingCount: uploadingCount, totalCount: array.count)
            completion(count)
        }
    }
    
    private func queryIsUploading(model: WmcUploadImageV2Model) -> Bool {
        var result = false
        
        for (_, item) in WmcUploadQueueManager.shared.awaitUploadQueue.enumerated() {
            if model.uuid == item.uuid {
                result = true
                break
            }
        }
        
        return result
    }
}
